package com.soft.mainsite.component.beetl.tag.content;

import cn.hutool.core.convert.Convert;
import com.soft.mainsite.common.exception.CmsException;
import com.soft.mainsite.module.web.cms.service.ContentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.beetl.core.GeneralVarTagBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Description:内容详情（特殊业务使用，查询出列表）
 *
 * @author Jin
 * @create 2017-06-06
 **/
@Slf4j
@Service
@Scope("prototype")
public class ContentDetailsTag extends GeneralVarTagBinding {

    @Autowired
    private ContentService contentService;

    @Value("${system.site.subfix}")
    private String siteSubfix;

    @Value("${system.site.prefix}")
    private String sitePrefix;

    @Override
    public void render() {
        String ids = Convert.toStr(this.getAttributeValue("ids"));
        Long modelId = Convert.toLong(this.getAttributeValue("modelId"));
        if (StringUtils.isBlank(ids)) {
            return;
        }
        String[] split = ids.split(",");
        List<Map> byModelIdAndContentIds = contentService.findByModelIdAndContentIds(modelId, split);
        try {
            wrapRender(byModelIdAndContentIds);
        } catch (Exception e) {
            log.error("内容标签渲染失败", e);
            throw new CmsException("内容标签渲染失败");
        }

    }

    private void wrapRender(List<Map> contentList) {
        int i = 1;
        for (Map content : contentList) {
            content.put("index", i);
            content.put("count", contentList.size());
            this.binds(content);
            this.doBodyRender();
            i++;
        }
    }
}
